var ProtoMessage    = require("./protoMessage");

var CurrentContext  = function() {
    this._origin        =   undefined;
    this._endpoint      =   undefined;
    this._adapter       =   undefined;
    this._servant       =   undefined;
    this._transceiver   =   undefined;
    this._timestamp     =   0;
    this._rspcontext    =   {};
}

module.exports  =   CurrentContext;

CurrentContext.prototype.__defineGetter__("application", function() { return this._adapter.handleImp.application; });
CurrentContext.prototype.__defineGetter__("communicator", function() { return this._adapter.handleImp.application.communicator; });

CurrentContext.prototype.getEndpoint    =   function() {
    return this._endpoint;
}

CurrentContext.prototype.getRequestId   =   function() {
    return this._origin ? this._origin.iRequestId : undefined;
}

CurrentContext.prototype.setResponseContext =   function(context) {
    this._rspcontext = context;
}

CurrentContext.prototype.getRequest =   function() {
    return this._origin;
}

CurrentContext.prototype.sendResponse   = function() {
    var response = new ProtoMessage.Response();
    response.origin = this._origin;
    response.appBuffer  =   Array.prototype.slice.call(arguments, 0);
    response.iResultCode    =   0;
    response.sResultDesc    =   "";

    this._transceiver.send(response);
}

CurrentContext.prototype.sendErrorResponse  = function(iRet, sMessage) {
    var response = new ProtoMessage.Response();
    response.origin =   this._origin;
    response.appBuffer  =   undefined;
    response.iResultCode    =   iRet;
    response.sResultDesc    =   sMessage;

    this._transceiver.send(response);
}

